/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *  
 *    http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License. 
 *  
 */
package org.apache.directory.studio.proxy.handlers;


import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Collection;

import org.apache.directory.ldap.client.api.message.LdapResult;
import org.apache.directory.shared.ldap.exception.LdapInvalidDnException;
import org.apache.directory.shared.ldap.message.ReferralImpl;
import org.apache.directory.shared.ldap.message.internal.InternalLdapResult;
import org.apache.directory.shared.ldap.message.internal.InternalReferral;
import org.apache.directory.shared.ldap.message.internal.InternalSearchResponseDone;
import org.apache.directory.shared.ldap.name.DN;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.handler.demux.MessageHandler;


/**
 * This class implements an abstract message handler.
 *
 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
 */
public abstract class AbstractMessageHandler<T> implements MessageHandler<T>
{
    /* (non-Javadoc)
     * @see org.apache.mina.handler.demux.MessageHandler#handleMessage(org.apache.mina.core.session.IoSession, java.lang.Object)
     */
    public void handleMessage( IoSession session, T message ) throws Exception
    {
        String ReceivedMsg = "\nMessage received:" + message.toString() + "Message Class:" + message.getClass()
            + "\n\n";
        System.out.println( ReceivedMsg );

        try
        {
            BufferedWriter out = new BufferedWriter( new FileWriter( "TestLogs", true ) );
            out.write( ReceivedMsg );
            out.close();
        }
        catch ( IOException e )
        {
        }

    };


    public void writeResponse( String response ) throws Exception
    {
        String FullResponse = "\nMessage sending:" + response;
        System.out.print( FullResponse );
        try
        {
            BufferedWriter out = new BufferedWriter( new FileWriter( "TestLogs", true ) );
            out.write( FullResponse );
            out.close();
        }
        catch ( IOException e )
        {
        }

    }

    /**
     * Forwards LDAP Result part of the incoming message to the respective outgoing message
     * @param inResult LDAP Result part of the incoming message
     * @param outResult LDAP result part of the outgoing message
     * @throws LdapInvalidDnException
     */
    public static void forwardLDAPResult( LdapResult inResult, InternalLdapResult outResult )
        throws LdapInvalidDnException
    {
        outResult.setResultCode( inResult.getResultCode() );

        if ( inResult.getMatchedDn() != null )
        {
            outResult.setMatchedDn( new DN( inResult.getMatchedDn() ) );
        }

        if ( inResult.getErrorMessage() != null )
        {
            outResult.setErrorMessage( inResult.getErrorMessage() );
        }
        if ( inResult.isReferral() )
        {
            InternalReferral ref = new ReferralImpl();
            Collection<String> ldapUrls = inResult.getReferral().getLdapUrls();
            for ( String url : ldapUrls )
            {

                ref.addLdapUrl( url );
            }

            outResult.setReferral( ref );
        }
    }

}
